Lab 3 - Create A Simple AI Agent.ipynb (153 lines of code) (raw):

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 2: Create a Simple AI Agent\n", "\n", "In this lab, we'll introduce you to AI agents by creating a simple agent that will create a bar graph based on data that we give to it. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Step 1: Load packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from typing import Any\n", "from pathlib import Path\n", "from dotenv import load_dotenv\n", "from azure.ai.projects import AIProjectClient\n", "from azure.identity import DefaultAzureCredential\n", "from azure.ai.projects.models import CodeInterpreterTool\n", "\n", "load_dotenv() # Load environment variables from .env file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Step 2: Connect to your Azure AI Foundry project" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Connecting to our Azure AI Foundry project, which will allow us to use the deployed gpt-4o model\n", "project_connection_string = os.getenv(\"AIPROJECT_CONNECTION_STRING\")\n", "model = os.getenv(\"CHAT_MODEL\")\n", "\n", "project_client = AIProjectClient.from_connection_string(\n", " conn_str=project_connection_string, credential=DefaultAzureCredential()\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Step 3: Create the simple AI Agent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with project_client:\n", " # Create an instance of the CodeInterpreterTool, which is responsible for generating the bar chart\n", " code_interpreter = CodeInterpreterTool()\n", "\n", " # The CodeInterpreterTool needs to be included in creation of the agent so that it can be used\n", " agent = project_client.agents.create_agent(\n", " model=model,\n", " name=\"my-agent-barchart\",\n", " instructions=\"You are a helpful agent.\",\n", " tools=code_interpreter.definitions,\n", " tool_resources=code_interpreter.resources,\n", " )\n", " print(f\"Created agent, agent ID: {agent.id}\")\n", "\n", " # Create a thread which is a conversation session between an agent and a user.\n", " thread = project_client.agents.create_thread()\n", " print(f\"Created thread, thread ID: {thread.id}\")\n", "\n", " # Create a prompt which contains the data + details for how the agent should generate the bar chart\n", " prompt = \"Could you please create a bar chart for the using the following data and \\\n", " provide the file to me? Name the file as health-plan-comparision.png. \\\n", " Here is the data: \\\n", " Provider\t Monthly Premium\tDeductible\tOut-of-Pocket Limit \\\n", " Northwind\t $300\t\t$1,500\t\t$6,000 \\\n", " Aetna\t\t $350\t\t$1,000\t\t$5,500 \\\n", " United Health\t$250\t\t$2,000\t\t$7,000 \\\n", " Premera\t\t $200\t\t$2,200\t\t$6,500 \\\n", " \"\n", " \n", " # Create a message, with the prompt being the message content that is sent to the model\n", " message = project_client.agents.create_message(\n", " thread_id=thread.id,\n", " role=\"user\",\n", " content=prompt,\n", " )\n", " print(f\"Created message, message ID: {message.id}\")\n", "\n", " # Run the agent to process tne message in the thread\n", " run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)\n", " print(f\"Run finished with status: {run.status}\")\n", "\n", " if run.status == \"failed\":\n", " # Check if you got \"Rate limit is exceeded.\", then you want to increase the token limit\n", " print(f\"Run failed: {run.last_error}\")\n", "\n", " # Get all messages from the thread\n", " messages = project_client.agents.list_messages(thread_id=thread.id)\n", " print(f\"Messages: {messages}\")\n", "\n", " # Generate an image file for the bar chart\n", " for file_path_annotation in messages.file_path_annotations:\n", " file_name = Path(file_path_annotation.text).name\n", " project_client.agents.save_file(file_id=file_path_annotation.file_path.file_id, file_name=file_name)\n", " print(f\"Saved image file to: {Path.cwd() / file_name}\")\n", "\n", " # Delete the agent once done\n", " project_client.agents.delete_agent(agent.id)\n", " print(\"Deleted agent\")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }